package com.unknown.wiki.details.view;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.widget.TextView;

import com.unknown.common.util.LogUtils;
import com.unknown.common.util.ToastUtils;
import com.unknown.wiki.R;
import com.unknown.wiki.base.BaseActivity;
import com.unknown.wiki.details.WikiDetailsContract;
import com.unknown.wiki.details.viewModel.WikiDetailsViewModel;
import com.unknown.wiki.model.Wiki;
import com.zzhoujay.richtext.RichText;

public class WikiDetailsActivity extends BaseActivity {
    private TextView tvContent;
    private WikiDetailsContract.IWikiDetailsViewModel wikiDetailsViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wiki_details);

        init();
        getWiki();
    }

    private void init() {
        tvContent = findViewById(R.id.wiki_details_content_tv);
        wikiDetailsViewModel = ViewModelProviders.of(this).get(WikiDetailsViewModel.class);
    }

    private void getWiki() {
        wikiDetailsViewModel.getWiki("").observe(this, this::onGetWiki);
    }

    private void onGetWiki(Wiki wiki) {
        if (wiki == null) {
            LogUtils.e("error occurs");
            ToastUtils.showShort(this, "oops, error occurs");
            return;
        }
        RichText.fromMarkdown(wiki.getContent()).into(tvContent);
    }

}
